package com.JZ.QM.controller;

import com.JZ.QM.common.entity.FdShareInfoEntity;
import com.JZ.QM.common.entity.ShareInfoEntity;
import com.JZ.QM.common.entity.StrategyEntity;
import com.JZ.QM.common.utils.CommonResult;
import com.JZ.QM.service.FdShareInfoService;
import com.JZ.QM.service.ShareInfoService;
import com.JZ.QM.service.StrategyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * @author lyp
 * @email lyp@gmail.com
 * @date 2020/6/15 16:54
 */
@RefreshScope
@RestController
@RequestMapping("/strategy")
@Slf4j
public class StrategyController {

    @Resource
    private StrategyService strategyService;
    @Resource
    private ShareInfoService shareInfoService;
    @Resource
    private FdShareInfoService fdShareInfoService;

    /**
     * 获取策略列表
     * @return
     */
    @RequestMapping("/allStrategyInfo")
    public CommonResult allStrategyInfo(){
        //策略列表
        List<StrategyEntity> strategyInfoList = strategyService.list();

        if (!strategyInfoList.isEmpty()){
            return CommonResult.success().put("strategyInfoList",strategyInfoList);
        }else {
            return CommonResult.error(201,"策略列表为空");
        }
    }

    /**
     * 根据策略id筛选
     * @param strategyId 策略id
     * @return
     */
    @RequestMapping("/strategyFiltrate")
    public CommonResult strategyFiltrate(Long strategyId){
        //根据主键获得策略实体
        StrategyEntity strategyEntity = new StrategyEntity();
        //股票列表
        List<ShareInfoEntity> shareInfoEntities = new ArrayList<>(0);
        //基金列表
        List<FdShareInfoEntity> fdShareInfoEntities = new ArrayList<>(0);

        //获得指定的策略基本信息
        strategyEntity = strategyService.getById(strategyId);

        if (strategyEntity == null){
            return CommonResult.error(201,"未查询到该策略列表");
        } else {
            //限定的股票数量不为0
            if (0 != strategyEntity.getShareLength()){

                //根据策略筛选条件获得股票列表
                shareInfoEntities = shareInfoService.filtrateShareList(
                        strategyEntity.getShareIndustry(),
                        strategyEntity.getShareMarket(),
                        strategyEntity.getSharePerformation()
                );

            }
            //限定的基金数量不为0
            if(0 != strategyEntity.getFdShareLength()){

                //根据策略筛选条件获得基金列表
                fdShareInfoEntities = fdShareInfoService.filtrateFdShareList(
                        strategyEntity.getFdShareInvestType(),
                        strategyEntity.getFdShareType(),
                        strategyEntity.getFdShareScaleMin(),
                        strategyEntity.getFdShareScaleMax()
                );

            }
            //无符合该策略的产品
            if (shareInfoEntities.isEmpty() && fdShareInfoEntities.isEmpty()){
                return CommonResult.error(201,"无符合该策略的产品");
            } else {
                return CommonResult.success()
                        .put("shareList",shareInfoEntities)
                        .put("fdShareList",fdShareInfoEntities)
                        .put("shareLength",strategyEntity.getShareLength())
                        .put("fdShareLength",strategyEntity.getFdShareLength()
                        );
            }
        }
    }
}
